package com.oriental.manage.service.specially.Impl;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.BeanUtils;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.manage.dao.specially.EnterpriseMchntBaseInfoMapper;
import com.oriental.manage.dao.specially.SpeciallyMchntBaseInfoDeleteMapper;
import com.oriental.manage.model.specially.EnterpriseMchntBaseInfo;
import com.oriental.manage.model.specially.SpeciallyMchntBaseInfoDelete;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.service.specially.BatchMessageService;
import com.oriental.manage.service.specially.SpeciallyMchntBaseInfoDeleteService;
import com.oriental.riskfront.api.enums.MessageStatus;
import com.oriental.riskfront.api.enums.TransCode;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by jinxin on 2017/5/4.
 */
@Service
@Slf4j
public class SpeciallyMchntBaseInfoDeleteServiceImpl implements SpeciallyMchntBaseInfoDeleteService {

    @Autowired
    SpeciallyMchntBaseInfoDeleteMapper speciallyMchntBaseInfoDeleteMapper;

    @Autowired
    EnterpriseMchntBaseInfoMapper enterpriseMchntBaseInfoMapper;

    @Autowired
    BatchMessageService batchMessageService;


    @Override
    public void search(SpeciallyMchntBaseInfoDelete speciallyMchntBaseInfoDelete, Pagination<SpeciallyMchntBaseInfoDelete> pagination) {
       List<SpeciallyMchntBaseInfoDelete> speciallyMchntBaseInfoDeleteList= speciallyMchntBaseInfoDeleteMapper.pageQuery(speciallyMchntBaseInfoDelete);
       if(speciallyMchntBaseInfoDeleteList!=null&&speciallyMchntBaseInfoDeleteList.size()!=0){
           pagination.setRowCount(speciallyMchntBaseInfoDeleteList.get(0).getRowCount());
           pagination.setList(speciallyMchntBaseInfoDeleteList);

       }
    }

//    @Override
//    public SpeciallyMchntBaseInfoDelete initSpeciallyMchntBaseInfoDelete(SpeciallyMchntBaseInfoDelete speciallyMchntBaseInfoDelete) {
//
//
//        EnterpriseMchntBaseInfo enterpriseMchntBaseInfo= new EnterpriseMchntBaseInfo();
//        enterpriseMchntBaseInfo.setCusCode(speciallyMchntBaseInfoDelete.getCusCode());
////       处理成功
//        enterpriseMchntBaseInfo.setUploadStatus("06");
//        List<EnterpriseMchntBaseInfo> enterpriseMchntBaseInfoList=enterpriseMchntBaseInfoMapper.searchEnterpriseMchntBaseInfo(enterpriseMchntBaseInfo);
//        SpeciallyMchntBaseInfoDelete s=BeanMapperUtil.objConvert(enterpriseMchntBaseInfoList.get(0),SpeciallyMchntBaseInfoDelete.class);
//        return s;
//    }
//
//    @Override
//    public List<SpeciallyMchntBaseInfoDelete> recordSpeciallyMchntBaseInfoDelete(SpeciallyMchntBaseInfoDelete speciallyMchntBaseInfoDelete) {
//        return  speciallyMchntBaseInfoDeleteMapper.searchSpeciallyMchntBaseInfoDelete(speciallyMchntBaseInfoDelete);
//    }

    @Override
    public void insertSpeciallyMchntBaseInfoDelete(SpeciallyMchntBaseInfoDelete speciallyMchntBaseInfoDelete, ResponseDTO<String> responseDTO) throws ApplicationException {
        EnterpriseMchntBaseInfo enterpriseMchntBaseInfo = new EnterpriseMchntBaseInfo();
        enterpriseMchntBaseInfo.setCusCode(speciallyMchntBaseInfoDelete.getCusCode());
        enterpriseMchntBaseInfo.setDeleteFlag("0");
        List<EnterpriseMchntBaseInfo> enterpriseMchntBaseInfoList=enterpriseMchntBaseInfoMapper.searchEnterpriseMchntBaseInfo(enterpriseMchntBaseInfo);
        if(enterpriseMchntBaseInfoList==null||enterpriseMchntBaseInfoList.size()==0){
            responseDTO.setSuccess(false);
            responseDTO.setMsg("该商户信息还未上报！");
            return;
        }else {
            EnterpriseMchntBaseInfo e=enterpriseMchntBaseInfoList.get(0);
            if("02".equals(e.getCenterStatus())){
                responseDTO.setSuccess(false);
                responseDTO.setMsg("该商户信息已经被删除！");
                return;
            }
            if("00".equals(e.getCenterStatus())){
                responseDTO.setSuccess(false);
                responseDTO.setMsg("该商户信息还未上报成功！");
                return;
            }
        }
        SpeciallyMchntBaseInfoDelete speciallyMchntBaseInfoDelete1 = new SpeciallyMchntBaseInfoDelete();
        speciallyMchntBaseInfoDelete1.setCusCode(speciallyMchntBaseInfoDelete.getCusCode());
        List<SpeciallyMchntBaseInfoDelete> speciallyMchntBaseInfoDeleteList=speciallyMchntBaseInfoDeleteMapper.searchSpeciallyMchntBaseInfoDelete(speciallyMchntBaseInfoDelete1);
        if(speciallyMchntBaseInfoDeleteList!=null&&speciallyMchntBaseInfoDeleteList.size()!=0){
            String uploadStatus= speciallyMchntBaseInfoDeleteList.get(0).getUploadStatus();
            //00: 初始 01: 审核通过 02: 审核拒绝 03:上报中 04:上报失败 05:处理失败 06:处理成功
            if(MessageStatus.PASS.getCode().equals(uploadStatus)||MessageStatus.SUB_INIT.getCode().equals(uploadStatus)){
                responseDTO.setSuccess(false);
                responseDTO.setMsg("该商户的上报信息正在处理中，");
                return;
            }else if(MessageStatus.INIT.getCode().equals(uploadStatus)){
                speciallyMchntBaseInfoDelete.setDeletePerson(SessionUtils.getUserName());
                speciallyMchntBaseInfoDelete.setDeleteTime(new Date());
                speciallyMchntBaseInfoDeleteMapper.updateSpeciallyMchntBaseInfoDelete(speciallyMchntBaseInfoDelete);
                responseDTO.setSuccess(true);
                return;
            }
        }
        speciallyMchntBaseInfoDelete.setUploadStatus(MessageStatus.INIT.getCode());
        speciallyMchntBaseInfoDelete.setDeletePerson(SessionUtils.getUserName());
        speciallyMchntBaseInfoDelete.setDeleteTime(new Date());
        speciallyMchntBaseInfoDelete.setId(UUID.randomUUID().toString().replace("-",""));
        BeanUtils beanUtils = new BeanUtils();
        beanUtils.nullConverNullString(speciallyMchntBaseInfoDelete);
        int info=speciallyMchntBaseInfoDeleteMapper.addSpeciallyMchntBaseInfoDelete(speciallyMchntBaseInfoDelete);
        if(info>0){
            BatchMessage batchMessage= new BatchMessage();
            batchMessage.setId(UUID.randomUUID().toString().replace("-",""));
            batchMessage.setSubmitMsgId(speciallyMchntBaseInfoDelete.getId());
            batchMessage.setStatus(MessageStatus.INIT.getCode());
            batchMessage.setDeleteFlag("0");
            batchMessage.setCount(1);
            batchMessage.setTrnxCode(TransCode.EDEL01.getCode());
            batchMessage.setReportDate(new Date());
            batchMessage.setCreateBy(SessionUtils.getUserName());
            batchMessage.setMerchant(enterpriseMchntBaseInfo.getCusCode());
            log.info("生成批次成功"+batchMessage);
            batchMessageService.insertBatchMessage(batchMessage);
        }
        responseDTO.setSuccess(true);

    }
}
